package wireless.frequent.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import cm.util.hibernate.HibernateSessionFactory;

import wireless.frequent.pojo.Frequent;

public class FrequentLengthDao {
    private String hql;
    private Query query;
    
	public Session getSession() {
		return HibernateSessionFactory.getSession();

	}
	@SuppressWarnings("unchecked")
	public List<Frequent> findAllDesc(int length,String orderType,
			int firstResult, int maxResult) {
		try {
			if(16==length)
			{
				hql = "from Frequent as f where f.length>8 order by f.id desc";
				query = getSession().createQuery(hql);
				
			}
			else{
				hql = "from Frequent as f where f.length=:l order by f.id desc";
				query = getSession().createQuery(hql);
				query.setInteger("l", length);
			}
			
			query.setFirstResult(firstResult);
			query.setMaxResults(maxResult);
			return query.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}
	public long countAll(int length) {
		try {
			if(16==length)
			{
				hql = "select count(*) from Frequent as f where f.length>8";
				query = getSession().createQuery(hql);
				
			}
			else{
				hql = "select count(*) from Frequent as f where f.length=:l";
				query = getSession().createQuery(hql);
				query.setInteger("l", length);
			}
			return (Long) query.uniqueResult();
		} catch (RuntimeException re) {
			throw re;
		}
	}
}	
